import { setItem, getItem } from '@/utils/storage'
import { LANG, TAGS_VIEW } from '@/constant'
export default {
  namespaced: true,
  state: {
    sliderbarOpen: true,
    language: getItem(LANG) || 'zh',
    tags: getItem(TAGS_VIEW) || []
  },

  mutations: {
    changeSliderbar(state) {
      state.sliderbarOpen = !state.sliderbarOpen
    },
    changeLanguage(state, language) {
      state.language = language
      setItem(LANG, language)
    },
    saveTags(state, tags) {
      const tag = state.tags.find((item) => item.path === tags.path)
      if (!tag) {
        state.tags.push(tags)
        setItem(TAGS_VIEW, state.tags)
      }
    },
    changeTags(state, { tag, index }) {
      state.tags[index] = tag
      setItem(TAGS_VIEW, state.tags)
    },
    closeTags(state, paylod) {
      if (paylod.type === 'other') {
        state.tags.splice(
          paylod.index + 1,
          state.tags.length - paylod.index + 1
        )
        state.tags.splice(0, paylod.index)
      } else if (paylod.type === 'right') {
        state.tags.splice(
          paylod.index + 1,
          state.tags.length - paylod.index + 1
        )
      } else if (paylod.type === 'current') {
        state.tags.splice(paylod.index, 1)
      }
      setItem(TAGS_VIEW, state.tags)
    }
  }
}
